package com.colaayi.youhelper.light;

import com.colaayi.youhelper.R;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Lightfragment extends Fragment {
	private ImageView imgv;
	private Camera camera;
	private Camera.Parameters parameters;
	private static int flag = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		camera = Camera.open();
		parameters = camera.getParameters();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contentview = inflater.inflate(R.layout.helper_light, null);
		final ImageView imgv = (ImageView) contentview.findViewById(R.id.light_img);
		
		imgv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (flag == 1) {
					if(camera==null){
						camera= Camera.open();
					}
					imgv.setBackgroundResource(R.drawable.power_on);
					parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
					camera.setParameters(parameters);
					
					flag = 2;
				} else {
					if (camera != null) {
						imgv.setBackgroundResource(R.drawable.power_off);
						parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
						camera.setParameters(parameters);
						//camera.stopPreview(); // 关掉亮灯
						//camera.release();
						//camera=null;
					}
					flag = 1;
				}
			}
		});

		return contentview;
	}



	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		camera.release();
		camera=null;
	}


}
